package com.pc.web.board;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.pc.web.member.MemberService;

@Controller
public class BoardController {
	@Autowired BoardDAO dao;
	@Autowired BoardService service;
	@Autowired MemberService Mservice;
		
	//글쓰기 폼 페이지
	@RequestMapping(value = "/board/writeForm", method = RequestMethod.GET)
	public String writeFormpage(){
		return "/board/board_writeForm";
	}
	//글쓰기 페이지
	@RequestMapping(value = "/board/write", method = RequestMethod.POST)
	public String writePage(HttpServletRequest request, PageBeans Pbeans, BoardBeans beans,Model model){
		//클라이언트 ip추적
		String board_ip=request.getRemoteAddr();
		beans.setBoard_ip(board_ip);
		dao.insert(beans);
		service.listpage(beans, Pbeans, model);
		return "/board/board_list";
	}
	//글 리스트 페이지
	@RequestMapping(value = "/board/list", method = {RequestMethod.GET, RequestMethod.POST})
	public String listPage(BoardBeans beans, PageBeans Pbeans, Model model){
		service.listpage(beans, Pbeans, model);
		return "/board/board_list";
	}
	
	//글 상세보기 페이지
	@RequestMapping(value = "/board/detail", method = {RequestMethod.GET, RequestMethod.POST})
	public String detailViewPage(String board_no, BoardBeans beans, HttpSession session, Model model){
		dao.countUpdate(board_no);
		service.view(board_no, beans, model);
		return "/board/board_detailView";
	}
	
	//글 수정상세 페이지
	@RequestMapping(value = "/board/modifyView", method = {RequestMethod.GET, RequestMethod.POST})
	public String modifyViewPage(String board_no, BoardBeans beans, Model model){
		beans=dao.modifyView(board_no);		
		model.addAttribute("beans", beans);
		return "/board/board_modifyView";
	}
	
	//글 수정 페이지
	@RequestMapping(value = "/board/modify", method = RequestMethod.POST)
	public String modifyPage(String board_no, BoardBeans beans, PageBeans Pbeans, Model model){
		dao.update(beans);
		model.addAttribute("beans", beans);
		service.listpage(beans, Pbeans, model);
		return "/board/board_list";
	}

	//글 삭제 페이지
	@RequestMapping(value = "/board/delete", method = RequestMethod.GET)
	public String deletePage(String board_no, BoardBeans beans, PageBeans Pbeans, Model model){
		dao.delete(board_no);
		service.listpage(beans, Pbeans, model);
		return "/board/board_list";
	}

	//댓글 삭제 페이지
	@RequestMapping(value = "/board/commentDelete", method = RequestMethod.POST)
	public String commentDelete(String board_no, String board_number, BoardBeans beans, PageBeans Pbeans, Model model){
		System.out.println("no = "+board_no);
		System.out.println("number = "+board_number);
		dao.commentDelete(board_number);
		dao.commentList(board_no);
		service.view(board_no, beans, model);
		return "/board/board_detailView";
	}

	//댓글 입력 및 리스트
	@RequestMapping(value = "/board/commentInsert", method = RequestMethod.POST)
	public String commentInsert(String board_number, String board_no, BoardBeans beans, Model model){
		dao.commentInsert(beans);
		service.view(board_no, beans, model);
		return "/board/board_detailView";
	}
}